package game;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;

import rps.RPSClient;

public class personajes extends JFrameConFondo {

	public personajes() {
		initComponents();
	}

	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		jPanel1 = new javax.swing.JPanel();
		personaje1 = new javax.swing.JButton();
		personaje2 = new javax.swing.JButton();
		personaje3 = new javax.swing.JButton();
		personaje4 = new javax.swing.JButton();
		jButton1 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Bowling Net");
		setResizable(false);
		
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"imagenes/b2.png"))); // NOI18N
		jButton2.setBorder(null);
		jButton2.setContentAreaFilled(false);
		jButton2.setPressedIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/b2_3.png"))); // NOI18N
		jButton2.setRolloverIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/b2_2.png"))); // NOI18N

		jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"imagenes/b3.png"))); // NOI18N
		jButton3.setBorder(null);
		jButton3.setContentAreaFilled(false);
		jButton3.setPressedIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/b3_3.png"))); // NOI18N
		jButton3.setRolloverIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/b3_2.png"))); // NOI18N

		jPanel1.setBackground(new java.awt.Color(255, 255, 255));
		jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		jPanel1.setOpaque(false);

		personaje1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"imagenes/per1.png"))); // NOI18N
		personaje1.setBorder(null);
		personaje1.setContentAreaFilled(false);
		personaje1.setRolloverIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/per1_2.png"))); // NOI18N

		personaje2.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"imagenes/per2.png"))); // NOI18N
		personaje2.setBorder(null);
		personaje2.setContentAreaFilled(false);
		personaje2.setRolloverIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/per2_2.png"))); // NOI18N

		personaje3.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"imagenes/per3.png"))); // NOI18N
		personaje3.setBorder(null);
		personaje3.setContentAreaFilled(false);
		personaje3.setRolloverIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/per3_2.png"))); // NOI18N

		personaje4.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"imagenes/per4.png"))); // NOI18N
		personaje4.setBorder(null);
		personaje4.setContentAreaFilled(false);
		personaje4.setRolloverIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/per4_2.png"))); // NOI18N

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout.createSequentialGroup().addGap(41, 41, 41)
						.addComponent(personaje1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 59,
								javax.swing.GroupLayout.PREFERRED_SIZE).addGap(
								30, 30, 30).addComponent(personaje2).addGap(18,
								18, 18).addComponent(personaje3,
								javax.swing.GroupLayout.PREFERRED_SIZE, 86,
								javax.swing.GroupLayout.PREFERRED_SIZE).addGap(
								18, 18, 18).addComponent(personaje4)
						.addContainerGap(73, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																personaje2, 0,
																0,
																Short.MAX_VALUE)
														.addComponent(
																personaje3, 0,
																0,
																Short.MAX_VALUE)
														.addComponent(
																personaje4,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																83,
																Short.MAX_VALUE)
														.addComponent(
																personaje1,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																83,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(348, 348, 348)));

		personaje1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				personaje1ActionPerformed(evt);
			}
		});
		personaje2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				personaje2ActionPerformed(evt);
			}
		});
		personaje3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				personaje3ActionPerformed(evt);
			}
		});
		personaje4.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				personaje4ActionPerformed(evt);
			}
		});

		jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"imagenes/b5.png"))); // NOI18N
		jButton1.setBorder(null);
		jButton1.setContentAreaFilled(false);
		jButton1.setPressedIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/b5_2.png"))); // NOI18N
		jButton1.setRolloverIcon(new javax.swing.ImageIcon(getClass()
				.getResource("imagenes/b5_3.png"))); // NOI18N

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(21, 21, 21).addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jButton2).addComponent(jButton3)
								.addComponent(jButton1)).addGap(47, 47, 47)
						.addComponent(jPanel1,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(72, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap(431, Short.MAX_VALUE)
										.addComponent(
												jButton2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												16,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jButton3,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												15,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jButton1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												12,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(76, 76, 76)).addGroup(
								layout.createSequentialGroup().addGap(85, 85,
										85).addComponent(jPanel1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(31, Short.MAX_VALUE)));

		// Code adding the component to the parent container - not shown here
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});
		jButton3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 800) / 2, (screenSize.height - 600) / 2,
				800, 600);
	}// </editor-fold>

	// GEN-END:initComponents

	public static void seleccionPersonaje() {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				JFrameConFondo jf = new personajes();
				// jf.setLocationRelativeTo(null);
				jf.setImagen("imagenes/personajes.png");
				jf.setVisible(true);

			}
		});
		InetAddress addr = null;
		String ipLocal = "";
		try {
			addr = InetAddress.getLocalHost();
			ipLocal = addr.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		leerArchivo();
		paramClient[0] = conexionServidor[0];
		paramClient[1] = ipLocal;
		HashMap mapa = new HashMap();
		mapa.put("puerto", conexionServidor[1]);
		client = RPSClient.clienteConsola(paramClient, mapa);

		if (client == null) {
			c = new configuracion();
			c.configurarServidor();
		}
	}

	public static void leerArchivo() {
		File archivo = null;
		FileReader fr = null;
		BufferedReader br = null;
		String nombreArchivo = "/configurarServer.txt";
		String baseClass = "configuracion";
		String path = "";
		try {
			path=System.getProperty("user.home").replaceAll("%20", " ");
			path+="/bowling";
			//path=System.getProperty("user.dir").replaceAll("%20", " ");
			// Apertura del fichero y creacion de BufferedReader para poder
			// hacer una lectura comoda (disponer del metodo readLine()).
			archivo = new File(path + System.getProperty("file.separator")
					+ nombreArchivo);
			System.out.println("el archivo es: " + archivo);
			fr = new FileReader(archivo);
			br = new BufferedReader(fr);

			// Lectura del fichero
			String linea;
			int i = 0;
			while ((linea = br.readLine()) != null) {
				conexionServidor[i] = linea;
				System.out.println(conexionServidor[i]);
				i++;
			}
		} catch (Exception e) {
			System.out.print("problema leyendo archivo de configuracion.");
		} finally {
			// En el finally cerramos el fichero, para asegurarnos
			// que se cierra tanto si todo va bien como si salta
			// una excepcion.
			try {
				if (null != fr) {
					fr.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		c = new configuracion();
		this.dispose();
		c.configurarServidor();
	}

	static String[] conexionServidor = new String[2];
	static String[] paramClient = new String[3];
	static configuracion c;
	static RPSClient client = null;
	private javax.swing.JButton jButton2;

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		i = new instrucciones();
		this.dispose();
		i.mostrarInstrucciones();
	}

	private javax.swing.JButton jButton3;

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
		r = new records();
		this.dispose();
		client.consultRecords();
	}

	records r;
	private javax.swing.JPanel jPanel1;
	instrucciones i;
	private javax.swing.JButton personaje1;

	private void personaje1ActionPerformed(java.awt.event.ActionEvent evt) {
		System.out.print(" antes de enviarlo " + client.getState());
		nick = new ingresarNick(client);
		this.dispose();
		nick.ingresoNick("imagenes/per1_3.png", client);
	}

	ingresarNick nick;
	private javax.swing.JButton personaje2;

	private void personaje2ActionPerformed(java.awt.event.ActionEvent evt) {
		nick = new ingresarNick();
		this.dispose();
		nick.ingresoNick("imagenes/per2_3.png", client);
	}

	private javax.swing.JButton personaje3;

	private void personaje3ActionPerformed(java.awt.event.ActionEvent evt) {
		nick = new ingresarNick();
		this.dispose();
		nick.ingresoNick("imagenes/per3_3.png", client);
	}
	
	private void formWindowClosing(java.awt.event.WindowEvent evt) {
		client.salir();
	}

	private javax.swing.JButton personaje4;

	private void personaje4ActionPerformed(java.awt.event.ActionEvent evt) {
		nick = new ingresarNick();
		this.dispose();
		nick.ingresoNick("imagenes/per4_3.png", client);
	}
	// End of variables declaration//GEN-END:variables

}
